The AKern Technical Documentation
    by AXEstudios

NOTE: This Documentation is meant for readers
    advanced in computers who want to understand
    the Kernel's inner workings.
      It is NOT a Guide or User's Manual.


        0. Table of Contents
    1 ....... Introduction
    1.1 ..... Overview of the Kernel
    2 ....... PART 1: The Startup Process
    3 ....... PART 2: The Global Descriptor Table



        1. Introduction
    This Documentation serves to explain every function and technical
aspect of the AKern Operating System Kernel as fully as possible.
    AKern stands for AXE's Kernel.

    This is regularly checked and maintained to fully inform the reader.

    AKern is a 32-bit x86 Operating System Kernel
designed to be fast on runtime but also easy to develop programs for.
    Right now, it only has support for IBM PS/2 keyboards.

    The kernel is completely written in NASM assembly language;
    Because it is more tedious to get to know a codebase written in
a language with many abstract concepts, like C.
    Right now, the kernel is very small.
    It currently (As far as I can tell), supports CPU interrupts,
has somewhat of a form of memory management,
memory reserved for system and userspace, and working screen output.

    The Kernel, and the boot-loader (called the AKern Loader),
have their own space in the first 19 sectors of the hard drive.
    (After the MBR Sector, which makes for 20 sectors)



        1.1 Overview of the Kernel
    The AKern Kernel is padded to be 18 sectors big, given that the bootloader
is one sectors big, so that the total space used by them both is 19 sectors.

    The only useful task the bootloader does is set the stack pointer.
    Once it loads, many other tasks have to be done by the Kernel, inlcuding
setting the A20 line.

    The first thing AKern does after being loaded is enable the A20 line and disable interrupts.
    Then, it switches to protected mode, from which it does all of it's other tasks.

    After entering protected mode, the Kernel checks the CPU to ensure compatiblility.

    Then, it sets up the PIC.

    After that, it sets up the CPU interrupts, and tests them.

    This is all it does.

    The only interrupt that exists right now is interrupt 18 at 0x12.
    It is used to test if interrupts are working.

    AKern also has some unused, (very simple) memory management routines;
    Not designed with multi-processing in mind.
    They have been tested to work, however.

    The Kernel's GDT has two segments.
    One for the system, and another for userland.
    The userland segment has not been tested yet.

    There is one used disk read routine.
    It supports IDE disks.



        2. PART 1: The Process
    The AKern Loader loads the Kernel via BIOS.
    Before loading the Kernel, it sets the stack pointer.
    The Kernel stack starts at the address 0x10ffff.

    The Kernel itself is loaded at the address 0x7e01.
    The size of the Kernel (with its padding), is 18 segments.

    Once the Kernel is loaded into memory, the boot-loader jumps
the the address 0x7e01.

    AKern, after being loaded into memory, and started,
enables the A20 line, loads the Global Descriptor Table,
and enters into protected mode.

    Once it is in protected mode, it does what is required, and
resets the stack pointer.

    After that, it does what is called the SETUP task.
    It is just like what any other Kernel does when it starts.
    The computer is checked and setup for compatiblility.
    On failure in any of the tests performed on the computer by
the SETUP task, (unless otherwise noted), an error message is displayed
and the computer hangs.

    The first thing the SETUP task does is check the CPU.
    Firstly in checking the CPU, it makes sure the CPU is 32-bit.

    After that, the CPU is checked if it supports the CPUID instruction.
    However, failure in this does not cause an error.

    Next, the CPUs Vendor ID string is obtained, dumped and checked.
    Also, not returning an error on failure.

    After all of this checking of the CPU is done, the next part
of the SETUP task begins.

    In this part, the PIC is initialized and setup.

    After that, the CPU interrupts are setup.
    This involves filling the Interrupt Descriptor Table with
a dummy interrup, and then adding the Interrupt Service Routines.
    After that, the interrupts are loaded and enabled.

    Once interrupts have been loaded and enabled, they are
TESTED to see if they work.

    Now that the local hardware has been setup, externel devices
are next.
    This simply involves testing some IDE disk read/write routines.

    Right now, the only routine that has been tested is the disk
read routine.

    The very last step after all of this is setting up usermode.

    This is very much a work in progress and is not functional right now.

    This is all that the SETUP task does.

    After all of this, the Kernel loops the HLT instruction.

        3. PART 2: The Global Descriptor Table
    
    AKern has a pretty generic Global Descriptor Table.
    It includes descriptors for the following segments:

        System-Reserved Code and Data segment
        "the General Memory" (userland, ring 3) Code and Data segment
        A Task State Segment descriptor.
    
    The only segment descriptor that is set at runtime is the
Task State Segment descriptor.

    There is only one Task State Segment.

    The System-Reserved memory segment is supposed to be 0x200000 bytes big
but due to limitations it is only 0x1fffff bytes big.

    The "General Memory" segment uses all the rest of memory.

    